package ar.edu.utn.frgp.mvc.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ar.edu.utn.frgp.banco.dao.Implements.ImpCuenta;
import ar.edu.utn.frgp.banco.domain.Cuenta;
import ar.edu.utn.frgp.beans.MessageBean;

/**
 * Servlet implementation class ConsultasCuentasServlet
 */
public class ConsultasCuentasServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ConsultasCuentasServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		MessageBean ban = new MessageBean();
		ImpCuenta cuentasdao = new ImpCuenta();
		List<Cuenta> lista_cuenta = new ArrayList<Cuenta>();
		

		try {
			int cl = Integer.parseInt(request.getSession().getAttribute("ID_Cliente").toString());
			
			lista_cuenta = cuentasdao.TraerCuentas(cl);
			if(lista_cuenta == null){
				throw new Exception("El cliente no posee cuentas."); //ENVIO DE ECEPCIONES
			}
			
			request.setAttribute("cuentas", lista_cuenta);
			ban.setMensaje("1");
			request.setAttribute("ban", ban);

			request.getRequestDispatcher("Cuentas.jsp").forward(request,
					response);

		} catch (Exception e) {
			MessageBean aux = new MessageBean();
			aux.setMensaje(e.getMessage());
			request.setAttribute("error", aux);
			request.getRequestDispatcher("Cuentas.jsp").forward(request,
					response);
		}

	}

}
